﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using System.Diagnostics;

namespace FindMusic
{
    public class Find
    {
        /// <summary>
        /// 要查找的文件后缀名,不带点儿的
        /// </summary>
        public string[] Filters { get; set; }

        public List<FileInfo> FindFile()
        {
            List<FileInfo> ls = new List<FileInfo>();
            foreach (var item in Environment.GetLogicalDrives())
            {
                ls.AddRange(GetAllFiles(item, Filters));
            }
            return ls;
        }

        public static List<FileInfo> GetAllFiles(string path, string[] filter)
        {
            List<FileInfo> ls = new List<FileInfo>();

            if (Directory.Exists(path))
            {
                //DirectorySecurity s = new DirectorySecurity(path, AccessControlSections.Access);
                //if (!s.AreAccessRulesProtected)
                //{
                DirectoryInfo di = new DirectoryInfo(path);
                foreach (DirectoryInfo item in di.GetDirectories())
                {
                    try
                    {
                        ls.AddRange(GetAllFiles(item.FullName, filter));
                    }
                    catch { continue; }
                }
                Console.WriteLine("正在查找 " + di.FullName);
                ls.AddRange(di.GetFiles());

                //}
            }

            ls.RemoveAll((str) =>
            {
                foreach (var item in filter)
                {
                    if (str.Extension == item) return false;
                }
                return true;
            });
            return ls;
        }


    }
}
